#ifndef GLOGIN_H
#define GLOGIN_H

#include<QThread>
#include<QObject>
#include<QString>
#include<QList>
#include<QDateTime>
#include<QTextStream>
#include<QFile>
#include<QStringList>
#include<QTimer>
#include<QImage>
#include<QPainter>
#include<QFont>

#include<stdlib.h>
//#include<jwsmtp/jwsmtp.h>


#include"GUser.h"
#include"GSupport.h"

#define PASSFILE		"users.pass"

//using namespace jwsmtp;

/*!	\brief This class is used to store user login data and mediate the friend list
 *	status updates.
*/
class GLoginMap : public QObject {
	Q_OBJECT
public:
	GLoginMap(QObject* parent) : QObject(parent), userptr(0), isverified(false) {}
	
	inline void name(QString n) { username = n; }
	inline QString name() { return username; }
	
	inline void pass(QString p) { userpass = p; }
	inline QString pass() { return userpass; }
	
	inline void email(QString e) { useremail = e; }
	inline QString email() { return useremail; }
	
	inline void user(GUser* a) { userptr = a; }
	inline GUser* user() { return userptr; }
	
	inline void created(QDateTime c) { sincecreated = c; }
	inline QDateTime created() { return sincecreated; }
	
	inline void lastLogin(QDateTime t) { lastlogin = t; }
	inline QDateTime lastLogin() { return lastlogin; }
	
	inline void verified(bool v) { isverified = v; }
	inline bool verified() { return isverified; }
	
	void initCreationTime();
	
	inline void status(QString str) { laststatus = str; }
	inline QString status() { return laststatus; }
	
	inline void initStatus() { emit sendStatus(laststatus); }
	
	/*!	\brief Name of this user.
	*/
	QString username;
	
	/*!	\brief Password for this user.
	*/
	QString userpass;
	
	/*!	\brief Email for this user.
	*/
	QString useremail;
	
	/*!	\brief Pointer to the GUser thread that is handling communications with this user.
	*/
	GUser* userptr;
	
	/*!	\brief Date when this account was created.
	*/
	QDateTime sincecreated;
	
	/*!	\brief Date when this user logged in last.
	*/
	QDateTime lastlogin;
	
	/*!	\brief Bool to specify if this account has been verified or not.
	*/
	bool isverified;
	
	/*!	\brief The status of the user when it was last set.
	*/
	QString laststatus;
	
public slots:
	void updateStatus(QString str) {
		laststatus = str;
		emit sendStatus(str);
	}
	
signals:
	//for friend list to send status updates
	void sendStatus(QString str);
	
};


/*!	\brief This thread manages the login data for users.
 *	
 *	This thread stores an array of GLoginMap which stores the actual
 *	user login data. This thread more or lest just saves and loads
 *	the user login data, receives requests from users to add an
 *	account, to login an existing account and to setup the friends
 *	list when the user logins in.
*/
class GLogin : public QThread {
	Q_OBJECT
public:
	GLogin(QObject* parent);
	
	
	static QString genRandString(int len);
	static void genRandImage(QString txt, QString fn);
	
	
	
private:
	QList<GLoginMap*> users;
	
	QTimer* verifytimer;
	QTimer* savetimer;
	QString lastsaved;
	
	QString firstLoad();
	
	bool changed;
	void save();
	void load();
	
	
signals:
	void doesNotExistStatus(QString str);
	
	
public slots:
	//this signal is received from a user thread to update his friend list
	int linkFriendList(GUser* user, GFriend* ptr, int index, QString name);
	
	//to login a user
	GResponse login(GUser* user, QString name, QString pass);
	//to logout
	void logout(GUser* user);
	//create a new account
	GResponse add(QString name, QString pass, QString email);
	
	void forgotPassword(QString user);
	
	/*!	\brief The function that is called when the verify timer timesout.
	 *	
	 *	If the GLoginMap::isverified field is not set to true for an account then the
	 *	account will be deleted if it is at least 30 days old.
	*/
	void checkVerifies();
	
	/*!	\brief The function that is called when the save timer timesout.
	 *	
	 *	If the account log was changed since it was last save it will be
	 *	saved otherwise it is not.
	*/
	void timerSave();
	
	GResponse createMessageChannel(GChatMap* ptr, QString user);
	
	GResponse createFileTransferChannel(GFileMap* ptr, GUser* cur, QString user);
	
	void setAccountVerified(QString user);
	
	void shutdown();
	void initialize();
	
};

#endif // GLOGIN_H
